﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace DiscoveryLogic.Workflows.Components.Markov.MaxEnt
{
    /// <summary>
    /// This ITrainingDataReader implementation will take care of reading a plain text file
    /// and returning the strings between each new line character, which is what
    /// many SharpEntropy applications need in order to create ITrainingEventReaders.
    /// </summary>
    public class PlainTextByLineDataReader : ITrainingDataReader<string>
    {
        private StreamReader mDataReader;
        private string mNextLine;

        /// <summary>
        /// Creates a training data reader for reading text lines from a file or other text stream.
        /// </summary>
        /// <param name="dataSource">
        /// StreamReader containing the source of the training data.
        /// </param>
        public PlainTextByLineDataReader(StreamReader dataSource)
        {
            mDataReader = dataSource;
            mNextLine = mDataReader.ReadLine();
        }

        /// <summary>
        /// Gets the next text line from the training data.
        /// </summary>
        /// <returns>
        /// Next text line from the training data.
        /// </returns>
        public virtual string NextToken()
        {
            string currentLine = mNextLine;
            mNextLine = mDataReader.ReadLine();
            return currentLine;
        }

        /// <summary>
        /// Checks if there is any more training data.
        /// </summary>
        /// <returns>
        /// true if there is more training data to be read.
        /// </returns>
        public virtual bool HasNext()
        {
            return (mNextLine != null);
        }
    }
}
